const path = require("path")
const db = require("../db")

module.exports.addArticle  = function(req,res){

    // console.log(req.body);
    // console.log("-----------");
    // console.log(req.file);

    // 1.数据的合法性的验证
    // 1.1 req.body
    // 1.2 req.file
    if(!req.file || req.file.fieldname!=="cover_img") return res.cc("请添加封面图片")

    const articleInfo = {
        ...req.body,
        cover_img: path.join('/uploads', req.file.filename), 
        pub_date: new Date(), 
        author_id: req.user.id
    }

    const sql = "insert into ev_articles set ?";
    db.query(sql,articleInfo,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows!==1) return res.cc('添加文章失败')
        res.cc('添加文章成功',0)
    })

}